#ifndef MAPSDISK_H_
#define MAPSDISK_H_


#include <iostream>
#include <map>
#include <string>
#include <vector>

#include <sstream>

#include <cstdio>

#include <fstream>

#include "WellSizedVector.h"
#include "PositionVector.h"


using namespace std;

class MapsDisk {
public:
	
	MapsDisk(){
		miniMapsInfo.miniMapCouter = 0;
		baseNameFile = "/work/miniMaps/miniMapsFile_";
		maxMiniMapsToRead = 600;
		
		numMinimpas = getNumberOfMinimaps();
	};

	~MapsDisk(){};
	
	string intToStr(unsigned long long num){
		stringstream sTemp;
		sTemp << hex << num << dec;
		return sTemp.str();
	}
	
	
	void openAllMiniMaps(){
		
		int numMinimpas = getNumberOfMinimaps();
		cout << "Abrindo total de minimapas:  " << numMinimpas << endl;
		
		inAllminiMaps = new ifstream [numMinimpas];
		checkedMiniMaps = new bool [numMinimpas];
		checkedMiniMapsEOF = new bool [numMinimpas];
		allBytesWSV = new unsigned long [numMinimpas];
		
		wsvToMerge = new WellSizedVector [numMinimpas];
		idToMerge = new unsigned long long [numMinimpas];
			
		string file_name;
		for(int i = 0; i <numMinimpas; i++){
			file_name = "/work/miniMaps/miniMapsFile_"; 
			file_name += intToStr(i);
			inAllminiMaps[i].open(file_name.c_str());
			checkedMiniMaps[i] = false;
			checkedMiniMapsEOF[i] = false;
		}
	}
	
	void closeAllMiniMaps(){
		int numMinimpas = getNumberOfMinimaps();
		cout << "Fechando todos os minimapas:  " << numMinimpas << endl;		
		for(int i = 0; i <numMinimpas; i++)
			inAllminiMaps[i].close();
		
		delete []wsvToMerge;
		delete [] idToMerge;
		delete [] inAllminiMaps;
		delete [] checkedMiniMaps;
		delete [] checkedMiniMapsEOF;
		delete [] allBytesWSV;
	}
	
	void DecodeString(WellSizedVector &_wsv, unsigned long long &_id, string _line, unsigned long & _allBytesWSV);
	
	void writeSuperMap(map<unsigned long long, WellSizedVector> & wordDictionary);
	bool getVector( unsigned long long & wordID, vector<WellSizedVector> & myVector, unsigned long long & incremento);
	void readSuperMap();
	
	unsigned int getNumberOfMinimaps(){
		unsigned int numberOfMiniMaps = 0;
		string file_name;
		FILE * checkFile = NULL;
		while(true){
			file_name = baseNameFile;
			file_name += intToStr(numberOfMiniMaps);
			checkFile = fopen(file_name.c_str(), "r");
			if(checkFile == NULL) break;
			fclose(checkFile); // Bizu
			checkFile = NULL;
			numberOfMiniMaps++;
			if(numberOfMiniMaps > maxMiniMapsToRead){
				cout << "Erro: Numero de minimapas muito grande, concaeneos..." << endl;
				cout << "Total de minimapas maximo: " << maxMiniMapsToRead << endl;
				exit(1);
			}	
		}
		return numberOfMiniMaps;
	}
			
private:
	struct _miniMapsInfo{
		unsigned int miniMapCouter;
	} miniMapsInfo;
	
	string baseNameFile;
	unsigned int maxMiniMapsToRead;
	
	ifstream * inAllminiMaps;
	bool * checkedMiniMaps;
	bool * checkedMiniMapsEOF;
	
	WellSizedVector * wsvToMerge;
    unsigned long long * idToMerge;
    
    int numMinimpas;
    unsigned long * allBytesWSV;
		
};


#endif /*MAPSDISK_H_*/
